package com.newenergy.wisehome.data.ormlite.dao;

import android.content.Context;

import com.j256.ormlite.dao.Dao;
import com.newenergy.wisehome.common.helper.LogHelper;
import com.newenergy.wisehome.data.bean.Icon;
import com.newenergy.wisehome.data.ormlite.db.DatabaseHelper;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


public class IconDao {
    private Context context;
    private Dao<Icon, Integer> iconDaoOpe;
    private DatabaseHelper helper;

    public IconDao(Context context) {
        this.context = context;
        try {
            helper = DatabaseHelper.getHelper(context);
            iconDaoOpe = helper.getDao(Icon.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 增加一个用户
     *
     * @param
     * @throws SQLException
     */
    public void add(Icon icon) {
        try {
            iconDaoOpe.create(icon);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }


    public Icon get(int id) {
        try {
            return iconDaoOpe.queryForId(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @return
     */
    public List<Icon> getIconsByType(int type) {
        List<Icon> iconList = new ArrayList<>();
        try {
            iconList = iconDaoOpe.queryBuilder().where().eq("type",
                    type).query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return iconList;
    }


    /**
     * @return
     */
    public List<Icon> getAll() {
        List<Icon> iconList = new ArrayList<>();
        try {
            iconList = iconDaoOpe.queryBuilder().query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return iconList;
    }


    public int delete(Icon icon) {
        try {
            return iconDaoOpe.delete(icon);
            //    LogHelper.i("deleIconnumber==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }


    public void deleAll() {
        try {
            int number = iconDaoOpe.deleteBuilder().delete();
             LogHelper.i("deleteAll number==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void delete(List<Icon> iconList) {
        try {
            int number = iconDaoOpe.delete(iconList);
            LogHelper.i("delete number==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


}
